package com.proxy.jdk;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 用于演示JDK动态代理
 * */
public class JDKProxyDemo implements InvocationHandler {

    private Object proxy;

    public JDKProxyDemo(Object proxy) {
        this.proxy = proxy;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理前置通知");
        Object res = method.invoke(this.proxy, args);
        System.out.println("代理后置通知");
        return res;
    }

    public static void main(String[] args) {
        PersonService personService = (PersonService) Proxy.newProxyInstance(PersonService.class.getClassLoader(),
                new Class[]{PersonService.class}, new JDKProxyDemo(new PersonServiceImpl()));
        personService.speak("jinxing");
    }
}
